package com.macro.mall.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.*;
import com.macro.mall.model.pms.PmsProductCategoryDO;
import com.macro.mall.model.pms.PmsProductCategoryQuery;
import com.macro.mall.service.PmsProductCategoryAttributeSetMappingDomainService;
import com.macro.mall.service.PmsProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品分类管理Controller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "PmsProductCategoryController")
@Tag(name = "PmsProductCategoryController", description = "商品分类管理")
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
    @Autowired
    private PmsProductCategoryService productCategoryService;


    @Autowired
    private PmsProductCategoryAttributeSetMappingDomainService productCategoryAttributeSetMappingDomainService;

    @ApiOperation("添加商品分类")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@Validated @RequestBody PmsProductCategoryParam productCategoryParam) {
        int count = productCategoryService.create(productCategoryParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改商品分类")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id,
                         @Validated
                         @RequestBody PmsProductCategoryParam productCategoryParam) {
        int count = productCategoryService.update(id, productCategoryParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询商品分类")
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<PmsProductCategoryDto>> getList(@PathVariable Long parentId,
                                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                        @RequestParam(value = "keyWord",required = false) String keyWord,
                                                                        @RequestParam(value = "isMappingAttrCategory",required = false) Integer isMappingAttrCategory
                                                                  ) {
        Page<PmsProductCategoryDO>  page = new Page<>(pageNum,pageSize);
        PmsProductCategoryQuery queryParam = new PmsProductCategoryQuery();
        queryParam.setParentId(parentId);
        queryParam.setName(keyWord);
        queryParam.setIsMappingAttrCategory(isMappingAttrCategory);
        Page<PmsProductCategoryDto> productCategoryPage = productCategoryService.getList(page,queryParam);
       if(CollectionUtils.isNotEmpty(productCategoryPage.getRecords())) {
           PmsProductCategoryAttrSetMappingQueryParam mappingQueryParam = new PmsProductCategoryAttrSetMappingQueryParam();
           mappingQueryParam.setCategoryIdList(productCategoryPage.getRecords().stream().map(PmsProductCategoryDto::getId).collect(Collectors.toList()));
           List<PmsProductCategoryAttributeSetMappingDto> setList = productCategoryAttributeSetMappingDomainService.listMappingSet(mappingQueryParam);
           if(CollectionUtils.isNotEmpty(setList)){
               Map<Long, List<PmsProductCategoryAttributeSetMappingDto>> categoryToSetMap =  setList.stream().collect(Collectors.groupingBy(PmsProductCategoryAttributeSetMappingDto::getCategoryId,Collectors.toList() ));
               productCategoryPage.getRecords().forEach(item -> {
                   if(categoryToSetMap.containsKey(item.getId())){
                       List<PmsProductCategoryAttributeSetMappingDto> categoryToSetList = categoryToSetMap.get(item.getId());
                       String setNames =categoryToSetList.stream().map(PmsProductCategoryAttributeSetMappingDto::getSetName).collect(Collectors.joining(","));
                       item.setAttributeSetNames(setNames);
                   }
               });
           }
       }
        return CommonResult.success(productCategoryPage);
    }

    @ApiOperation("根据id获取商品分类")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsProductCategoryDO> getItem(@PathVariable Long id) {
        PmsProductCategoryDO productCategory = productCategoryService.getItem(id);
        return CommonResult.success(productCategory);
    }

    @ApiOperation("删除商品分类")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = productCategoryService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改导航栏显示状态")
    @RequestMapping(value = "/update/navStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateNavStatus(@RequestParam("ids") List<Long> ids, @RequestParam("navStatus") Integer navStatus) {
        int count = productCategoryService.updateNavStatus(ids, navStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改显示状态")
    @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateShowStatus(@RequestParam("ids") List<Long> ids, @RequestParam("showStatus") Integer showStatus) {
        int count = productCategoryService.updateShowStatus(ids, showStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("查询所有一级分类及子分类")
    @RequestMapping(value = "/list/withChildren", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProductCategoryWithChildrenItemDto>> listWithChildren() {
        List<PmsProductCategoryWithChildrenItemDto> list = productCategoryService.listWithChildren();
        return CommonResult.success(list);
    }

    @ApiOperation("查询所有一级分类及子分类")
    @RequestMapping(value = "/tree/allCategoryTree", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProductCategoryTreeItemDto>> getAllCategoryTree() {
        List<PmsProductCategoryTreeItemDto>  list = productCategoryService.listAllCategoryTree();

        return CommonResult.success(list);
    }

}
